<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="/WEB-INF/displaytag-12.tld" prefix="display" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../css/sample1.css" rel="stylesheet" type="text/css">
<style type="text/css">
	<!--
	body {
		background-image: url(../images/main_bg2.gif);
	}
	.style2 {color: #88DB23}
	.style3 {color: #FFFFFF}
	-->
</style>
<script>
	function GetXmlHttpObject(handler)
	{
		var objXmlHttp=null
		if (navigator.userAgent.indexOf("Opera")>=0)
		{		      
			alert("This doesn't work in Opera")
			return
		}
		if (navigator.userAgent.indexOf("MSIE")>=0)
		{
			var strName="Msxml2.XMLHTTP"
			if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
			{
				strName="Microsoft.XMLHTTP"
			}
			try
			{
				objXmlHttp=new ActiveXObject(strName)
				objXmlHttp.onreadystatechange=handler
				return objXmlHttp
			}
			catch(e)
			{
				alert("Error. Scripting for ActiveX might be disabled")
				return
			}
		}
		if (navigator.userAgent.indexOf("Mozilla")>=0)
		{
			objXmlHttp=new XMLHttpRequest()
			objXmlHttp.onload=handler
			objXmlHttp.onerror=handler
			return objXmlHttp
		}
	}		 		
	function getResult(adsid)
	{
		row = adsid;	
		url = "../jsp/SearchOutput.jsp?adid="+adsid;
		obj = GetXmlHttpObject(resultText);
		obj.open("GET",url,true);
		obj.send(null);
	}	  
	function resultText()
	{
		if(obj.readyState==4 || obj.readyState=="complete")
		{	
			document.getElementById("result").innerHTML=obj.responseText;      	 	  
		}  
	}
	function changeStatus()
	{
		if(document.getElementById('listPict').checked) 
		{
			document.getElementById('listPict').value = "on";
		} 
		else 
		{
			document.getElementById('listPict').value = "off";
		}
	
	}
	function goBack() 
	{
		var url;
		<%
			String cnt = (String) session.getAttribute("back");
			if("index".equals(cnt)) {
		%>
				url = "../Classifieds";
		<%
			} 
			else 
			{
		%>
				url = "../jsp/SearchClassifieds.jsp?PostingType=" + "<%=request.getParameter("PostingType")%>";
			<%
				if(null!=request.getParameter("CatType")) 
				{
			%>
					url = url + "&CatType=" + "<%=request.getParameter("CatType")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("pricemin")) 
				{
			%>
					url = url + "&pricemin=" + "<%=request.getParameter("pricemin")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("pricemax")) 
				{
			%>
					url = url + "&pricemax=" + "<%=request.getParameter("pricemax")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("excludeSearchContent")) 
				{
			%>
					url = url + "&excludeSearchContent=" + "<%=request.getParameter("excludeSearchContent")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("adContent")) 
				{
			%>
					url = url + "&adContent=" + "<%=request.getParameter("adContent")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("City")) 
				{
			%>
					url = url + "&City=" + "<%=request.getParameter("City")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("City")) 
				{
			%>
					url = url + "&City=" + "<%=request.getParameter("City")%>";
			<%
				}
			%>
			<%
				if(null!=request.getParameter("listPict")) 
				{
			%>
					url = url + "&listPict=" + "<%=request.getParameter("listPict")%>";
			<%
				}
			%>
	<%
			}
	%>

			window.location.href = url;
	}
</script>
<SCRIPT LANGUAGE="JavaScript">
	function checkNum(fieldName, fieldVal, fieldLen)
	{
	       //Check whether the value of the field is a number
	    if (isNaN(fieldVal)==true || (fieldLen!=fieldVal.length && fieldLen!=0))
	    {
	        //if it is not a number display a message
	        if (fieldLen==0)
	         {
	            //Message for variable length numbers
	            document.form1.elements[fieldName].focus();
	            alert("This must be a number.");
	
	            return false;
	        }
	        else
	        {
	            //Message for fixed length fields
	            document.form1.elements[fieldName].focus();
	            alert("This must be a " + fieldLen + " digit number.");
	
	            return false;
	        }
	    }
	}
</script>

</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<jsp:include page="top.jsp"/>
<br>
	<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
		<tr>
			<td>
				<table width="40%" border="0" align="left" cellpadding="5" cellspacing="5" >
					<tr>
          					<td colspan="5">
					            <%
						    	java.util.Hashtable allArea = (java.util.Hashtable)application.getAttribute("APALLCATEGORY");
						    	java.util.Hashtable allAreaId = (java.util.Hashtable)application.getAttribute("APCATIDS");
						    	String catType =request.getParameter("CatType");
						    	session.setAttribute("CatType",catType);
						    	int catId;
						    	String mainCatId;
						       	String subCatName;
						    	String mainCatName="";
						    	int postingID=0;
						    	
						    	if (catType!=null)
						    	{
						    		catId =  Integer.parseInt(catType);
						    	} 
						    	else 
						    	{
						    		catId=0;
						    	}
					
					    		if (catId==0)
					    		{
						    		subCatName="";
						    		mainCatId = request.getParameter("PostingType");
						    		
					    			if (mainCatId!=null && mainCatId.length()>0)
					    			{
									postingID = Integer.parseInt(mainCatId);
								}
					    			session.setAttribute("PostingType",mainCatId);
					    			
					    			if (mainCatId.equals("99"))
					    			{
					    				mainCatName="All Categories";
					    			}
					    			else
					    			{
					    				mainCatName = (String)allArea.get(mainCatId);
					    			}
					    		}
					    		else
					    		{
							    	mainCatId = (String)allAreaId.get(catId+"");
							       	subCatName = (String)allArea.get(catId+"");
							       	mainCatName = (String)allArea.get(mainCatId);
						       	}				
					    	    %>
            					   <form name="form1" method="post" action="../portal/SearchAdv.do?action=SearchAd&PostingType=<%=mainCatId%>&CatType=<%=catId%>">
						              <table width="100%" cellpadding="3" cellspacing="0" border="0" bgcolor="#99CC33">
						                <!--DWLayoutTable-->
							               <tr bgcolor="#CCFF33">
								                  <td valign="top" colspan="6" class="title" height="24" nowrap>
									                    <% if (mainCatName.equals("All Categories"))
									                       {
													mainCatName="";
											    %>
									                    		<font size="3"><font color="#000000" class="boldtext">Search all Categories: </font><b><font color="#000000"></font></b></font>
									                    <% }
									                       else 
									                       {
									                    %>
									                    		<font size="3"><font color="#000000" class="boldtext">Search in this Category: </font><b><font color="#000000"></font></b></font>
									                    <% } %>
								                  </td>
							                </tr>
											                <%
													  	String pricemin = request.getParameter("pricemin");
														if (pricemin==null)
															pricemin="";
															session.setAttribute("pricemin",pricemin);
															String pricemax = request.getParameter("pricemax");
															
														if (pricemax==null)
															pricemax="";
															session.setAttribute("pricemax",pricemax);
															String adContent = request.getParameter("adContent");
															String excludeSearchContent= request.getParameter("excludeSearchContent");
															
														if (adContent==null)
															adContent="";
															session.setAttribute("adContent",adContent);
															
														if (excludeSearchContent==null)
															excludeSearchContent="";
															session.setAttribute("excludeSearchContent",excludeSearchContent);
															session.setAttribute("City",request.getParameter("City"));
															session.setAttribute("listPict",request.getParameter("listPict"));
													  %>
                											  <%    if (mainCatId.equals("1") || mainCatId.equals("2")) 
                											  	{
                											  %>
													                <tr bgcolor="#FFFFFF">
														                  <td height="28" align="right" nowrap class="text">Price Min:
														                  </td>
														                  <td width="144">
														                    	<input type="text" name="pricemin"  value="<%=pricemin%>"  onchange="return checkNum('pricemin', pricemin.value, 0)">
														                  </td>
														                  <td width="100" nowrap class="text">Price Max:</td>
														                  <td colspan="3">
														                   	<input type="text" name="pricemax"  value="<%=pricemax%>" onBlur="return checkNum('pricemax', pricemax.value, 0)">
														                  </td>
													                </tr>
               												 <%	}	%>
												                    	<tr bgcolor="#FFFFFF">
														                  <td height="28" align="right" nowrap class="text">Search Words:</td>
														                  <td>
														                    	<input type="text" name="adContent" value="<%=adContent%>">
														                  </td>
														                  <td align="right"><span class="text">Exclude Words:</span></td>
														                  <td colspan="2">
														                  	<input name="excludeSearchContent" type="text" id="excludeSearchContent" value="<%=excludeSearchContent%>">
																  </td>
														                  <td>&nbsp;
														                  </td>
												                	</tr>
															 <tr bgcolor="#FFFFFF">
																<td valign="top" class="text">City</td>            
																<td>
																      <select name="City">	                    
																      		<option value="0">All</option>	                    
																      		<%							
																      		  java.sql.Connection conn = null;
																      		  java.sql.Statement  stat = null;
																      		  int city_id = 0;
																      		  
																      		  if(null != request.getParameter("city_id") && !"".equals(request.getParameter("city_id"))) 
																      		  {
																      		  	city_id = Integer.parseInt(request.getParameter("city_id"));
																      		  }	
																      		      Class.forName("com.mysql.jdbc.Driver");
																      		      conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/ba3602_droikclassifieds","ba3602_portal", "p0rtal");
																      		      //conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/ba3602_droikclassifieds","root","" );
																      		      stat = conn.createStatement();	                    	java.sql.ResultSet rs = stat.executeQuery("select city_id, cityname from lookup_city;");	                    	int cityid;	                    	String cityname;	    					while (rs.next()){	    							    						cityid = rs.getInt(1);	    						cityname = rs.getString(2);	    			     %>	    			     		<option <%if(city_id==cityid) out.println("selected");%> value="<%=cityid%>"><%=cityname%></option>	    			     %>	    			     <%	    			      	}	    			     %>	                  </select>            </td>
													
													                  			<td class="text">With Pic
																		  	<%
																				if("on".equals(request.getParameter("listPict"))) 
																				{
																		  	%>
																		  			<input type="checkbox" checked name="listPict" onClick="javascript:changeStatus()">
																		  	<%
																		  		} 
																		  		else 
																		  		{
																		  	%>
																		  			<input type="checkbox" name="listPict" onClick="javascript:changeStatus()">
																			<%
																		  		}
																		  	%>
																		</td>
																		<td align="right" valign="top">
																		 	<input name="Search" type="submit" value="Search">
																		</td>
													                			<td width="84">&nbsp;</td>
													                  			<td>&nbsp;</td>
											                		 </tr>
													                 <tr bgcolor="#FFFFFF">
														                  <td height="47" colspan="6" nowrap class="title"><br>
														                   	 <%=mainCatName%><font size="1"><b> <img src="../images/arrow.gif" width="13" height="13"></b></font><%=subCatName%>
														                  </td>
													                </tr>
															<% int index = 0; %>
													                <% if (mainCatId.equals("1") || mainCatId.equals("2")) 
													                   {%>
															  	<%if (catType==null || catType.equals("0"))
															  	  { %>
																	<tr bgcolor="#FFFFFF">
															                  	<td colspan="6" valign="top"  height="44">
																			   	<br>
																					<table class="simple">
																					<tr>
																					<th class="title_link sortable">
																					<a href="/jsp/DisplayAd.jsp?d-4719132-s=0&amp;excludeSearchContent=&amp;action=SearchAd&amp;Action=Search&amp;d-4719132-o=2&amp;city_id=&amp;adContent=&amp;CatType=0&amp;PostingType=99&amp;city=">Featured Postings</a>
																					</th>
																					</tr>
																					<c:forEach items="${sessionScope.SEARCHRESULT}" var="CRList">
																					<% if(index%2==0)  { %>
																					<tr class="odd">
																					<% } else { %>
																					<tr class="even">
																					<% 
																					   }
																					   index++;
																					 %>
																					<td>
																						<table>
																							<tr>
																								<td>	
																									<a href='javascript:getResult(<c:out value="${CRList.advId}"/>);'><c:out value="${CRList.adTitle}" /></a>
																								</td>
																							</tr>
																							<tr>
																								<td>
																									<c:out value="${CRList.city}" />, <c:out value="${CRList.subCategoryName}" />, <c:out value="${CRList.price}" />
																								</td>
																							</tr>
																						</table>
																					</td>   
																					</tr>  
																					</c:forEach>
																					</table>
															                   	 </td>
               																 </tr>
																<%}
																  else
																  { %>
																	<tr bgcolor="#FFFFFF">
																		<td colspan="6" valign="top"  height="44">
																			<br>
																			<table class="simple">
																			<tr>
																			<th class="title_link sortable">
																			<a href="/jsp/DisplayAd.jsp?d-4719132-s=0&amp;excludeSearchContent=&amp;action=SearchAd&amp;Action=Search&amp;d-4719132-o=2&amp;city_id=&amp;adContent=&amp;CatType=0&amp;PostingType=99&amp;city=">Featured Postings</a>
																			</th>
																			</tr>
																			<c:forEach items="${sessionScope.SEARCHRESULT}" var="CRList">
																			<% if(index%2==0)  { %>
																			<tr class="odd">
																			<% } else { %>
																			<tr class="even">
																			<% 
																			   }
																			   index++;
																			 %>
																			<td>
																				<table>
																					<tr>
																						<td>	
																							<a href='javascript:getResult(<c:out value="${CRList.advId}"/>);'><c:out value="${CRList.adTitle}" /></a>
																						</td>
																					</tr>
																					<tr>
																						<td>
																							<c:out value="${CRList.city}" />, <c:out value="${CRList.subCategoryName}" />, <c:out value="${CRList.price}" />
																						</td>
																					</tr>
																				</table>
																			</td>   
																			</tr>  
																			</c:forEach>
																			</table>
																		</td>
																	</tr>
                														<%}
                													   } 
                													   else 
                													   { %>
															 <%if (catType==null)
															   {%>
																	<tr bgcolor="#FFFFFF">
																		<td colspan="6" valign="top"  height="25">
																			<table class="simple">
																			<tr>
																			<th class="title_link sortable">
																			<a href="/jsp/DisplayAd.jsp?d-4719132-s=0&amp;excludeSearchContent=&amp;action=SearchAd&amp;Action=Search&amp;d-4719132-o=2&amp;city_id=&amp;adContent=&amp;CatType=0&amp;PostingType=99&amp;city=">Featured Postings</a>
																			</th>
																			</tr>
																			<c:forEach items="${sessionScope.SEARCHRESULT}" var="CRList">
																			<% if(index%2==0)  { %>
																			<tr class="odd">
																			<% } else { %>
																			<tr class="even">
																			<% 
																			   }
																			   index++;
																			 %>
																			<td>
																				<table>
																					<tr>
																						<td>	
																							<a href='javascript:getResult(<c:out value="${CRList.advId}"/>);'><c:out value="${CRList.adTitle}" /></a>
																						</td>
																					</tr>
																					<tr>
																						<td>
																							<c:out value="${CRList.city}" />, <c:out value="${CRList.subCategoryName}" />, <c:out value="${CRList.price}" />
																						</td>
																					</tr>
																				</table>
																			</td>   
																			</tr>  
																			</c:forEach>
																			</table>   
																		</td>
																	</tr>
															<%}
															  else
															  {%>
																	<tr bgcolor="#FFFFFF">
																		<td colspan="6" valign="top"  height="25">
																			<table class="simple">
																			<tr>
																			<th class="title_link sortable">
																			<a href="/jsp/DisplayAd.jsp?d-4719132-s=0&amp;excludeSearchContent=&amp;action=SearchAd&amp;Action=Search&amp;d-4719132-o=2&amp;city_id=&amp;adContent=&amp;CatType=0&amp;PostingType=99&amp;city=">Featured Postings</a>
																			</th>
																			</tr>
																			<c:forEach items="${sessionScope.SEARCHRESULT}" var="CRList">
																			<% if(index%2==0)  { %>
																			<tr class="odd">
																			<% } else { %>
																			<tr class="even">
																			<% 
																			   }
																			   index++;
																			 %>
																			<td>
																				<table>
																					<tr>
																						<td>	
																							<a href='javascript:getResult(<c:out value="${CRList.advId}"/>);'><c:out value="${CRList.adTitle}" /></a>
																						</td>
																					</tr>
																					<tr>
																						<td>
																							<c:out value="${CRList.city}" />, <c:out value="${CRList.subCategoryName}" />, <c:out value="${CRList.price}" />
																						</td>
																					</tr>
																				</table>
																			</td>   
																			</tr>  
																			</c:forEach>
																			</table>
																		</td>
																	</tr>
                												       <% }
                												     }%>
																	<tr>
																		<td height="0" width="103"></td>
																		<td width="144"></td>
																		<td></td>
																		<td width="54"></td>
																		<td></td>
																		<td width="159"></td>
																	</tr>
															                <tr bgcolor="#FFFFFF">
															                    <td  colspan="6" align="right">
															                	<input type="button" name="Back" value="Back" onClick="javascript:goBack();" class="button"/>
															                    </td>
															                </tr>
							      </table>
						   </form>
          				</td>
        			</tr>
      			</table>
    		</td>
		<td width="60%">
			<div id="result">
			</div>
		</td>
  	</tr>
	<tr>
	</tr>	
</table>
 <blockquote>
   <p><br>
      <br>
     <jsp:include page="footer.html"/>
	 <jsp:include page="map.html"/>
   </p>
 </blockquote>
</body>
</html>
<!---
<script type="text/javascript">
	var table = document.getElementById("classfiedads");
	var rows = table.getElementsByTagName("tr");
	for (i=2; i < rows.length; i++) {
	rows[i].onmouseover = function() { this.style.cursor='pointer'};
	rows[i].onclick = function(){ getResult(i) };
	}
</script>
-->